//
//  TabBarUtils基本设置
//  Created by 小柠檬🍋 on 2025/9/11.
//

import Foundation
import SwiftUI

class TabBarUtils{
    
    static func setCommonBar() {
        DispatchQueue.main.async {
            UITabBar.appearance().unselectedItemTintColor = UIColor(Color.hex("#C2C2C2"))
            // 配置底部TabBar
            let tabBarAppearance = UITabBarAppearance()
            
            // 设置背景色（不透明）
            tabBarAppearance.configureWithOpaqueBackground()
            tabBarAppearance.backgroundColor = UIColor(Color.bgDialog)
            // 去除阴影和分隔线
            tabBarAppearance.shadowImage = UIImage()
            tabBarAppearance.shadowColor = nil
            // 应用配置
            UITabBar.appearance().standardAppearance = tabBarAppearance
            if #available(iOS 15.0, *) {
                UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
            }
            // 强制刷新当前的TabBar
            if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
               let rootViewController = windowScene.windows.first?.rootViewController as? UITabBarController {
                rootViewController.tabBar.reloadInputViews()
                rootViewController.tabBar.layoutIfNeeded()
            }
        }
        
    }

    //更新TabBar，并且是否设置透明
    static func updateTabBarAppearance(_ isTransparent: Bool) {
        DispatchQueue.main.async {
            // 获取当前显示的TabBar实例
            guard let tabBar = self.getCurrentTabBar() else { return }
            
            // 配置外观
            let appearance = UITabBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.backgroundColor = isTransparent ?
                UIColor(Color.clear) :
                UIColor(Color.bgDialog)
            appearance.shadowImage = UIImage()
            appearance.shadowColor = nil
            
            // 直接设置到TabBar实例
            tabBar.unselectedItemTintColor = UIColor(Color.hex("#C2C2C2"))
            tabBar.standardAppearance = appearance
            if #available(iOS 15.0, *) {
                tabBar.scrollEdgeAppearance = appearance
            }
            
            // 强制刷新
            tabBar.setNeedsLayout()
            tabBar.layoutIfNeeded()
        }
    }

    // 辅助方法：获取当前窗口的TabBar实例
    private static func getCurrentTabBar() -> UITabBar? {
        if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
           let rootVC = windowScene.windows.first?.rootViewController {
            // 递归查找TabBarController
            return findTabBar(in: rootVC)
        }
        return nil
    }

    private static func findTabBar(in viewController: UIViewController) -> UITabBar? {
        if let tabBarController = viewController as? UITabBarController {
            return tabBarController.tabBar
        }
        for child in viewController.children {
            if let tabBar = findTabBar(in: child) {
                return tabBar
            }
        }
        return nil
    }

}
